/* ===== order =====：预订 */
myApp.onPageInit('order', function (page) {
    //每个可选日期的价格
    var content = [];
    //可选日期
    var daylist = [];
    _contents = $$("#order .contentinfo");
    for(var i=0;i<_contents.length;i++){
        _content = _contents[i];       
        daylist.push($$(_content).data("date"));
        content[$$(_content).data("date")] = {
            pricebasic:$$(_content).data("pricebasic"),
            priceservice:$$(_content).data("priceservice"),
        }
    }
    //行程天数
    var days = $$("#order .days").data("days");
    
    //初始化行程日期
    // console.log(days,content);
    $$("#order .start-time").val(daylist[0]);
    $$("#order .end-time").val(getthedate(daylist[0],parseInt(days)));
    //初始化价格
    var startday = $$("#order .start-time").val();
    var persons = $$("#order .mui-input-numbox").val();
    var price = parseFloat(content[startday].pricebasic) + parseFloat(content[startday].priceservice)*parseInt(persons);
    $$("#order .price").html(price.toString());
    
    //出发时间改变
    $$("#order .start-time").on("change",function(){
        var startday = $$("#order .start-time").val();
        var persons = $$("#order .mui-input-numbox").val();
        //console.log(persons);
        var price = parseFloat(content[startday].pricebasic) + parseFloat(content[startday].priceservice)*parseInt(persons);
        //console.log(price,typeof(price));
        $$("#order .price").html(price.toString());
        $$("#order .end-time").val(getthedate(startday,parseInt(days))); 
    });
    
    //人数增加
    $$('#order .mui-btn-numbox-plus').on('click',function(){
        var peoplenum = $$('#order .mui-input-numbox').val();
        $$('#order .mui-input-numbox').val(parseInt(peoplenum)+1);
        //重新计算价格
        var startday = $$("#order .start-time").val();
        var persons = $$("#order .mui-input-numbox").val();
        var price = parseFloat(content[startday].pricebasic) + parseFloat(content[startday].priceservice)*parseInt(persons);
        $$("#order .price").html(price.toString());
    });
    //人数减少
    $$('#order .mui-btn-numbox-minus').on('click',function(){
        var peoplenum = $$('#order .mui-input-numbox').val();
        if(peoplenum>1){
           $$('#order .mui-input-numbox').val(parseInt(peoplenum)-1);
           var startday = $$("#order .start-time").val();
           var persons = $$("#order .mui-input-numbox").val();
           var price = parseFloat(content[startday].pricebasic) + parseFloat(content[startday].priceservice)*parseInt(persons);
           $$("#order .price").html(price.toString());
        }                
    });
    
    //初始化datepicker
    var monthNames = ['01', '02', '03', '04', '05', '06', '07', '08' , '09' , '10', '11', '12'];
    var dayNamesShort = ['日','一','二','三','四','五','六'];
    var starttime = myApp.calendar({
        input: '.start-time',
        monthNames: monthNames,
        dayNamesShort:dayNamesShort,
        weekHeader: true,
        dateFormat: 'yyyy-MM-dd',
        /*onDayClick: function (p,dayContainer,year,month,day) {
        	year=year.toString();
        	month=parseInt(month)+2;
        	month=month.toString();
        	day=day.toString();

        	console.log(year,typeof(year),month,typeof(month),day,typeof(day));
        	var daySelected=[year+"-"+month+"-"+day];
        	console.log(daySelected)
        	starttime.setValue(daySelected);
        	console.log(starttime.value);
        	
        },*/
        onOpen: function (p) {            
            //设定可选时间
            $$('.picker-calendar-months div.picker-calendar-day').addClass('picker-calendar-day-disabled');
            for(var j=0;j<daylist.length;j++){
                var _day = getformatday(daylist[j]);
                var dom = '.picker-calendar-months div[data-date="' + _day +'"]';
                $$(dom).removeClass('picker-calendar-day-disabled');
            }			
        },
        onMonthYearChangeStart: function (p) {
        	//设定可选日期
            $$('.picker-calendar-months div.picker-calendar-day').addClass('picker-calendar-day-disabled');
            for(var j=0;j<daylist.length;j++){
                var _day = getformatday(daylist[j]);
                var dom = '.picker-calendar-months div[data-date="' + _day +'"]';
                $$(dom).removeClass('picker-calendar-day-disabled');
            }   
        }
    });
    
    //下单
    $$("#order .charge").on("click",function(){
        console.log("charge");
        var goodsId = $$(this).data("id");
        var type = $$(this).data("type");
        var name = $$(this).data("name");
        var guiderIcon = $$(this).data("guiderIcon");
        var guiderName = $$(this).data("guiderName");
        var price = $$("#order .price").html();
        var persons = $$("#order .mui-input-numbox").val();
        var startday = $$("#order .start-time").val();
        var stopday = $$("#order .end-time").val();
        var xing = $$("#order input[name='xing']").val();
        var ming = $$("#order input[name='ming']").val();
        var code = $$("#order input[name='city']").val();
        var tel = $$("#order input[name='tel']").val();
        console.log(goodsId,type,price,persons,startday,stopday,xing,ming,code,tel);
        $$.getJSON(url_get_addorder, {
            goodsId:goodsId,   //产品id
            price:price,
            persons:persons, //人数
            startDay:startday,     
            stopDay:stopday,
            type:type,      //TRACK,ACTIVITY,ZIYING
            touristXing:xing,        //下单者姓氏
            touristMing:ming,       //下单者名字
            touristAreaCode:code,    //电话区号
            touristTelNum:tel,      //下单者电话
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                var transId = ajaxresult.data.transId;
                var context = {
                    "transId":transId,
                    "guiderIcon":guiderIcon,
                    "guiderName":guiderName,
                    "name":name,
                };
                mainView.router.load({
                    "url":"charge.html",
                    "context":context
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
                var context = {
                    "transId":0,
                    "guiderIcon":guiderIcon,
                    "guiderName":guiderName,
                    "name":name,
                };
                mainView.router.load({
                    "url":"charge.html",
                    "context":context
                }); 
            }
        });
    })
});

/* ===== charge =====：支付 */
myApp.onPageInit('charge', function (page) {
    //!!!!!!!!!微信支付
    $$("#charge .pay").on("click",function(){
        var transId = $$(this).data("id");
        $$.getJSON(url_get_alipay, {'transId':transId}, function (ajaxresult) {
            if(ajaxresult.code==1){
                myApp.alert("支付成功！");
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
});

/* ===== myorder ===== ：我的订单*/
myApp.onPageInit('myorder', function (page) {
    console.log('myorder');
    var loading = false;
    var completed_page = 1;
    var uncomplete_page = 1;
    var limit = 10;

    $$('#myorder .tab .row a').click(function(){
        $$('#myorder .tab .col-50_active').removeClass('col-50_active');
        myApp.attachInfiniteScroll($$('#myorder .infinite-scroll'));
        $$(this).parent().addClass('col-50_active');
    });
    $$('#myorder .infinite-scroll').on('infinite', function () {
        console.log("scroll");
        $$('#myorder .infinite-scroll-preloader').show();
        if (loading) return;
        loading = true;
        setTimeout(function () {
            loading = false;
            if($$('#myorder .tab .col-50_active').attr("name")=='completed'){
                completed_page++;
                $.getJSON(url_get_myorder, {
                    size:limit,// 每页显示数量
                    page:completed_page,// 页码
                    status:1// (1 有效单 0 退款单)
                }, function (ajaxresult) {
                    if(ajaxresult.code==1){
                        if(ajaxresult.data != false){
                            var data_myorderlist = ajaxresult.data.data;
                            var html_myorderlist = myApp.myorderlistTemplate(data_myorderlist);
                            $$('#myorder #completed ul').append(html_myorderlist);
                        }else{
                            myApp.detachInfiniteScroll($$('#myorder .infinite-scroll'));
                            $$('#myorder .infinite-scroll-preloader').hide();
                            return;
                        }                        
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                });

            }else{
                uncomplete_page++;
                $$.getJSON(url_get_myorder, {
                    size:limit,// 每页显示数量
                    page:uncomplete_page,// 页码
                    status:0// (1 有效单 0 退款单)
                }, function (ajaxresult) {
                    if(ajaxresult.code==1){
                        if(ajaxresult.data != false){
                            var data_myorderlist = ajaxresult.data.data;
                            var html_myorderlist = myApp.myorderlistTemplate(data_myorderlist);
                            $$('#myorder #uncomplete ul').append(html_myorderlist);

                        }else{
                            myApp.detachInfiniteScroll($$('#myorder .infinite-scroll'));
                            $$('#myorder .infinite-scroll-preloader').hide();
                            return;
                        }
                        
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                });
            }
      }, 1000);
    }); 
});

/* ===== orderprocess =====：我的预定 */
myApp.onPageInit('orderprocess',function(page){
    var completed_page = 1;
    var uncomplete_page = 1;
    var limit = 10;
	$$('#orderprocess .tab .row a').click(function(){
		$$('#orderprocess .tab .col-50_active').removeClass('col-50_active');
        myApp.attachInfiniteScroll($$('#orderprocess .infinite-scroll'));
		$$(this).parent().addClass('col-50_active');
	});

    $$('#orderprocess .process').on('click', function (e) {
        var id = $$(this).data("id");
        var actionSheetButtons = [
            [
                {
                    text: '接受',
                    onClick: function (id) {
                        //处理预定
                        $$.getJSON(url_post_dealorder, {
                            id:id,// 订单号
                            action:1,// 操作 (1接受 / 2拒绝)
                            reason:"",// 拒绝理由
                        }, function (ajaxresult) {
                            if(ajaxresult.code==1){
                                myApp.alert("订单已接受！");
                            }else{
                                ajaxfail(ajaxresult.msg);
                            }
                        });
                    }
                },
                // Another red button
                {
                    text: '拒绝',
                    //color: 'red',
                    onClick: function (id) {
                        mainView.router.load({
                            'url':'orderreject.html',
                            'context':{
                                'id':id
                            }
                        });
                    }
                },
            ],
            // Second group
            [
                {
                    text: '取消',
                    bold: true
                }
            ]
        ];
        myApp.actions(actionSheetButtons);
    });
    $$('#orderprocess .infinite-scroll').on('infinite', function () {
        console.log("scroll");
        $$('#orderprocess .infinite-scroll-preloader').show();
        if (loading) return;
        loading = true;
        setTimeout(function () {
            loading = false;
            if($$('#orderprocess .tab .col-50_active').attr("name")=='completed'){
                completed_page++;
                $$.getJSON(url_get_orderlist, {
                    size:limit,// 每页显示数量
                    page:completed_page,// 页码
                    iscomment :1
                }, function (ajaxresult) {
                    if(ajaxresult.code==1){
                        if(ajaxresult.data != false){
                            var data_orderlist = ajaxresult.data.data;
                            var html_orderlist = myApp.orderprocess_1Template(data_orderlist);           
                            $$('#orderprocess #completed ul').append(html_orderlist);
                        }else{
                            myApp.detachInfiniteScroll($$('#orderprocess .infinite-scroll'));
                            $$('#orderprocess .infinite-scroll-preloader').hide();
                            return;
                        }
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                });
            }else{
                uncomplete_page++;
                $$.getJSON(url_get_orderlist, {
                    size:limit,// 每页显示数量
                    page:uncomplete_page,// 页码
                    iscomment :0// (1 有效单 0 退款单)
                }, function (ajaxresult) {
                    if(ajaxresult.code==1){
                        if(ajaxresult.data != false){
                            var data_orderlist = ajaxresult.data.data;
                            var html_orderlist = myApp.orderprocess_2Template(data_orderlist);
                            $$('#orderprocess #uncomplete ul').append(html_orderlist);
                        }else{
                            myApp.detachInfiniteScroll($$('#orderprocess .infinite-scroll'));
                            $$('#orderprocess .infinite-scroll-preloader').hide();
                            return;
                        }
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                });
            }
      }, 1000);
    });
});

/* ===== orderreject =====：拒绝订单 */
myApp.onPageInit('orderreject',function(page){
    $$("#orderreject .orderreject_submit").on("click",function(){
        var id = $$(this).data("id");
        var reason = $$("#orderreject textarea[name='reason']").val();
        $$.getJSON(url_post_dealorder, {
            id:id,// 订单号
            action:2,// 操作 (1接受 / 2拒绝)
            reason:reason,// 拒绝理由
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                myApp.alert("订单已拒绝！");
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
});

/* ===== orderdetail_guider =====：预订详情 */
myApp.onPageInit('orderdetail_guider',function(page){
    $$("#orderdetail .msg").on("click",function(){
        var rev_id = $$(this).data("id");
        $$.getJSON(url_get_chat, {
            rev_id:rev_id,// 接收对象id
            page:1,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"msg.html",
                    "context":ajaxresult.data
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
});


/* ===== orderdetail =====：预订详情 */
myApp.onPageInit('orderdetail',function(page){
    //申请退款
    $$("#orderdetail .refund").on("click",function(){
        var orderid = $$(this).data("id");
        $$.post(url_post_refundorder, {
            order_id:orderid,   //产品id
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                myApp.alert("退款成功");
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });

    $$("#orderdetail .msg").on("click",function(){
        var rev_id = $$(this).data("id");
        $$.getJSON(url_get_chat, {
            rev_id:rev_id,// 接收对象id
            page:1,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"msg.html",
                    "context":ajaxresult.data
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
    
    //完成行程
    $$("#orderdetail .finish").on("click",function(){
        var orderid = $$(this).data("id");
        $$.post(url_post_finishorder, {
            order_id:orderid,   //产品id
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                myApp.alert("形成已完成!");
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
});